//
//  DiscoverViewController.m
//  Mtime
//
//  Created by wang xinkai on 15/7/28.
//  Copyright (c) 2015年 wxk. All rights reserved.
//

#import "DiscoverViewController.h"
#import "NewsTableView.h"//新闻列表
#import "NewsModel.h"//新闻数据

#import "NoticeTableView.h"//预告片列表
#import "NoticeModel.h"//预告片数据

#import "ChartsTableView.h"//排行榜列表
#import "ChartsModel.h"//排行榜数据

#import "CommentTableView.h"//评论列表
#import "CommentModel.h"//评论数据

@implementation DiscoverViewController
{

    NewsTableView *_newsTableview;//新闻列表
    NSMutableArray *_newsDataList;//新闻数据

    NoticeTableView *_noticeTableView;//预告片列表
    NSMutableArray *_noticeDataList;//预告片数据
    
    ChartsTableView *_chartsTableView;//排行榜列表
    NSMutableArray *_chanrtsDataList;//排行榜数据
    
    CommentTableView *_commentTableView;//影评列表
    NSMutableArray *_commentDataList;//影评数据
    
    SegmentView *seg;
    
}
-(id)init{
    
    if (self = [super init]) {
        
        self.title = @"发现";
        self.tabBarItem.image = [UIImage imageNamed:@"discover"];

    }
    return self;
    
}

- (void)creatSegmentView {

    seg = [[SegmentView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 44)WithTitiles:@[@"新闻",@"预告片",@"排行榜",@"影评"]];
    
    seg.backgroundColor = [UIColor clearColor];
    
    __weak DiscoverViewController *weakSelf = self;
    [seg addBlcok:^(NSInteger index) {
        
        __strong DiscoverViewController *strongSelf = weakSelf;
        [strongSelf segmentViewAction:index];
        
    }];
    self.navigationItem.titleView = seg;
}

- (void)segmentViewAction:(NSInteger)index {

    if (index == 0) {
        
        [self.view bringSubviewToFront:_newsTableview];
    }else if (index == 1) {
        
        [self.view bringSubviewToFront:_noticeTableView];
    }else if (index == 2) {
        
        [self.view bringSubviewToFront:_chartsTableView];
    }else {
        
        [self.view bringSubviewToFront:_commentTableView];
    }

}

- (void)loadData {
    
    /*-----------------新闻数据------------------*/
    _newsDataList = [[NSMutableArray alloc] init];
    
    NSDictionary *newsDic = [CoreDataSource initWithDataSource:@"find_news"];
    
    NSArray *newsList = [newsDic objectForKey:@"newsList"];
    
    for (NSDictionary *dic in newsList) {
        
        NewsModel *model = [[NewsModel alloc] initWithDic:dic];
        
        [_newsDataList addObject:model];
    }
    
    
    /*-----------------预告片数据------------------*/
    _noticeDataList = [[NSMutableArray alloc] init];
    NSDictionary *noticeDic = [CoreDataSource initWithDataSource:@"notice"];
    
    NSArray *trailers = [noticeDic objectForKey:@"trailers"];
    
    for (NSDictionary *dic in trailers) {
        
        NoticeModel *model = [[NoticeModel alloc] initWithDic:dic];
        
        [_noticeDataList addObject:model];
    }
    
    /*------------------排行榜数据-------------------*/
    _chanrtsDataList = [[NSMutableArray alloc] init];
    NSDictionary *charts = [CoreDataSource initWithDataSource:@"rank2"];
    
    NSArray *topLists = [charts objectForKey:@"topLists"];
    
    for (NSDictionary *dic in topLists) {
        
        ChartsModel *model = [[ChartsModel alloc] initWithDic:dic];
        [_chanrtsDataList addObject:model];
        
    }

    /*------------------影评数据-------------------*/
    _commentDataList = [[NSMutableArray alloc] init];
    NSArray *commnetArray = [CoreDataSource initWithDataSource:@"criticism"];
    
    for (NSDictionary *dic in commnetArray) {
        
        CommentModel *model = [[CommentModel alloc] initWithDic:dic];
        [_commentDataList addObject:model];
        
    }

    
}

- (void)viewDidLoad {

    [super viewDidLoad];
    
    [self creatSegmentView];//添加segmentvie导航栏
    
    [self loadData];//加载数据
    
    [self creatTableView];//加载列表视图
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noticeAction) name:@"notice" object:nil];
    
}

- (void)noticeAction {

    seg.selectedIndex = 1;
}

- (void)creatTableView {

    //新闻列表
    _newsTableview = [[NewsTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH -64)];
    
    _newsTableview.dataList = _newsDataList;
    
    [self.view addSubview:_newsTableview];
    
    //预告片列表
    _noticeTableView = [[NoticeTableView alloc] initWithFrame:_newsTableview.bounds];
    
    // 取出数据中的第一个元素  传递给_noticeTableView作为头部视图的数据  并在setModel的方法中加载头部视图
    _noticeTableView.model = _noticeDataList[0];
    
    //把数据中的第一个元素移除掉
    [_noticeDataList removeObjectAtIndex:0];
    
    //把数据传递给_noticeTableView
    _noticeTableView.dataList = _noticeDataList;
    
    [self.view addSubview:_noticeTableView];
    
    
    //排行榜列表
    _chartsTableView = [[ChartsTableView alloc] initWithFrame:_newsTableview.bounds];
    
    _chartsTableView.dataList = _chanrtsDataList;
    
    [self.view addSubview:_chartsTableView];

    
    //影评列表
    _commentTableView = [[CommentTableView alloc] initWithFrame:_newsTableview.bounds];
    
    _commentTableView.dataList = _commentDataList;
    
    [self.view addSubview:_commentTableView];
    
    //将新闻列表放到所有列表的最前面
    [self.view bringSubviewToFront:_newsTableview];

}



@end
